//
//  NSString+Add.m
//  PigMarket
//
//  Created by Felix on 2019/4/12.
//  Copyright © 2019 felix.pigmarket.oc. All rights reserved.
//

#import "NSString+Add.h"

@implementation NSString (Add)
/**
 判断是否是空字符串
 @return BOOL
 */
- (BOOL)isBlankString {
    if ([self isEqual:[NSNull null]] || (self == nil) || (self == NULL)) {
        return YES;
    }
    
    if ([self isKindOfClass:[NSNull class]]) {
        return YES;
    }
    if (![self pureString].length) {
        return YES;
    }
    return NO;
}
/**
 计算字符串的长度
 
 @return 长度
 */
- (NSInteger)unicodeSize {
    NSInteger len = 0;
    char *p = (char*)[self cStringUsingEncoding:NSUnicodeStringEncoding];
    for (int i = 0; i < [self lengthOfBytesUsingEncoding:NSUnicodeStringEncoding]; i++) {
        if (*p) {
            p++;
            len++;
        } else {
            p++;
        }
    }
    return len;
}

/**
 是否是纯整形
 
 @return BOOL
 */
- (BOOL)isPureInt {
    NSScanner *scanner = [NSScanner scannerWithString:self];
    NSInteger anInter;
    BOOL scan = [scanner scanInteger:&anInter];
    BOOL end = [scanner isAtEnd];
    return scan && end;
}

/**
 去掉前后和换行的空格
 
 @return NSString
 */
- (NSString *)pureString {
    return [self stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
}

/**
 转为以http开始的字符串
 
 @return NSString
 */
- (NSString *)convertStringToURL {
    if (self == nil) {
        return nil;
    }
    NSString *tempString = [self copy];
    if (![self hasPrefix:@"http"]) {
        tempString  = [NSString stringWithFormat:@"http://%@",tempString];
    }
    return tempString;
}

/**
 是否含有某个字符串
 
 @param string 字符串
 @return BOOL
 */
- (BOOL)ifContainsString:(NSString*)string {
    NSRange range = [self rangeOfString:string];
    return range.length != 0;
}

/**
 是否是有效的手机号码
 
 @return BOOL
 */
- (BOOL)isValidPhoneNumber {
    if ([self length] == 0) {
        return NO;
    }
    if ([self isPureInt]) {
        NSString *regex = @"^1[0-9]{10}$";
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
        return [predicate evaluateWithObject:self];
    } else {
        return NO;
    }
}

/**
 是否有效的密码8~16位
 
 @return BOOL
 */
- (BOOL)isValidPassword {
    NSString *regex = @"^[a-zA-Z0-9]{8,16}$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    return [predicate evaluateWithObject:self];
}

/**
 是否有效验证码
 
 @return BOOL
 */
-(BOOL)isValidVerify {
    NSString *regex = @"^[0-9]{6}$";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    return [predicate evaluateWithObject:self];
}

/**
 是否是邮箱格式
 
 @return BOOL
 */
- (BOOL)isValidEmail {
    if ([self length] == 0) {
        return NO;
    }
    NSString *regex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}";
    NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
    return [predicate evaluateWithObject:self];
}

/**
 根据字体大小,最大宽度，算出Size
 
 @param font 字体大小
 @param maxWidth 最大宽度
 @return Size
 */
- (CGSize)sizeWithFont:(UIFont *)font maxWidth:(CGFloat)maxWidth {
    NSMutableDictionary *attributes = [NSMutableDictionary dictionary];
    attributes[NSFontAttributeName] = font;
    
    CGSize maxSize = CGSizeMake(maxWidth, MAXFLOAT);
    
    if([[UIDevice currentDevice].systemVersion floatValue] >= 7.0) {
        return [self boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size;
    } else {
        return [self sizeWithFont:font maxWidth:maxWidth];
    }
}

/**
 根据字体大小,算出Size
 
 @param font 字体大小
 @return Size
 */
- (CGSize)sizeWithFont:(UIFont *)font {
    return [self sizeWithFont:font maxWidth:MAXFLOAT];
}


+ (CGSize)sizeWithHtml:(NSString *)htmlString{
    
    NSAttributedString *attrStr = [[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
    
    UILabel *label = [[UILabel alloc] init];
    label.frame = CGRectMake(0, 0, KScreenWidth - 30, 300);
    label.attributedText = attrStr;
    CGRect textOfRect = [label textRectForBounds:CGRectMake(0, 0, KScreenWidth - 30, CGFLOAT_MAX) limitedToNumberOfLines:0];
    return textOfRect.size;
}


/**
 时间戳转换时间
 
 @param timeStamp 时间戳
 @return 转换后的时间
 */
+ (NSString *)transferTimeStamp:(double)timeStamp {
    NSTimeInterval interval = timeStamp;
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:interval];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSString *dateStr = [formatter stringFromDate:date];
    return dateStr;
}
@end
